home *** CD-ROM | disk | FTP | other *** search
- /*
- File: OTGetDefaultEthernetAddress.c
-
- Contains: Demo of accessing OT from Symantec C for MPW 68K code resource.
-
- Written by: Quinn "The Eskimo!"
-
- Copyright: © 1996 by Apple Computer, Inc., all rights reserved.
-
- Change History (most recent first):
-
- */
-
- #include <OpenTransport.h>
- #include <OpenTptLinks.h>
- #include <LibraryManagerUtilities.h>
- #include <LibraryManager.h>
- #include <HyperXCMD.h>
- #include <TextUtils.h>
- #include <Strings.h>
- #include <string.h>
- #include <stdio.h>
-
- // Prototypes
-
- static void TrueMain(XCmdPtr xpb);
-
- // Standard XCMD main entry point
-
- pascal void MAIN(XCmdPtr xpb)
- {
- OSStatus err;
- GlobalWorld old_world;
-
- // Debugger();
- old_world = GetCurrentGlobalWorld();
- err = InitCodeResource();
- if (err != noErr) {
- // No point using a DebugStr because the string would A5 relative
- // and this failure indicates that A5 could not be setup!
- Debugger();
- } else {
- TrueMain(xpb);
-
- (void) SetCurrentGlobalWorld(old_world);
- FreeGlobalWorld();
- };
- // Debugger();
- }
-
- // Generic OT stuff
-
- typedef struct {
- char bytes[6];
- } EnetAddress, *EnetAddressPtr;
-
- typedef struct T8022Address T8022Address;
-
- static T8022Address simple_addr = {
- AF_8022, // OTAddressType for 802 provider
- {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // hardware address, use zeros for bind requests
- 0x8888, // SAP / protocol type, value > 1500 implies ethernet protocol with no SNAP
- {0x00,0x00,0x00,0x00,0x00}
- };
-
- static OSStatus GetDefaultEthernetAddress(EnetAddressPtr def_enet_addr)
- {
- EndpointRef ep;
- OSStatus err;
- TBind bind_request;
- TBind bind_result;
- T8022Address theReturnAddr;
-
- ep = OTOpenEndpoint(OTCreateConfiguration(kEnetName), 0, 0, &err);
- if (err == kOTNoError) {
- // finish bind request
- bind_request.addr.buf = (UInt8 *) &simple_addr;
- bind_request.addr.len = k8022BasicAddressLength; // family type + Ethernet + type field
- bind_request.addr.maxlen = 0;
- bind_request.qlen = 0;
- // setup bind result
- bind_result.addr.buf = (UInt8 *) &theReturnAddr;
- bind_result.addr.len = 0;
- bind_result.addr.maxlen = sizeof(theReturnAddr);
- bind_result.qlen = 0;
- // call bind
- err = OTBind(ep, &bind_request, &bind_result);
- if (err == noErr) {
- BlockMove((Ptr) theReturnAddr.fHWAddr, (Ptr) def_enet_addr->bytes, 6);
- // clean up
- err = OTUnbind(ep);
- }
- err = OTCloseProvider(ep);
- }
- return (err);
- }
-
- static void TrueMain(XCmdPtr xpb)
- // Extra procedure wrapper to avoid horrible register caching problem
- // whereby the value of result was held in register A3 which was
- // setup from an A5 relative offset before our A5 had been set up
- // by InitCodeResource. Urgh!
- {
- OSStatus err;
- EnetAddress def_enet_addr;
- char result[256];
- char tmpstr[3];
- short i;
-
- err = InitOpenTransport();
- if (err == noErr) {
- err = GetDefaultEthernetAddress(&def_enet_addr);
- if (err == noErr) {
- result[0] = 0;
- for (i = 0; i < 6; i++) {
- tmpstr[0] = "0123456789ABCDEF"[(def_enet_addr.bytes[i] >> 4) & 0x0F];
- tmpstr[1] = "0123456789ABCDEF"[def_enet_addr.bytes[i] & 0x0F];
- tmpstr[2] = 0;
- strcat(result, (char *) tmpstr);
- if (i < 5) {
- strcat(result, ":");
- };
- };
- };
-
- CloseOpenTransport();
- };
-
- if (err != noErr) {
- strcpy(result, "Error");
- };
-
- (void) PtrToHand((Ptr) result, &xpb->returnValue, strlen(result) + 1);
- }
-